package com.zmn.oms.admin.controller.work.modify;

import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dto.work.modify.must.ZsCheckOutDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.modify.must.ZsCheckOutVO;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.financework.ZsNormalFinanceWorkBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 类描述：工单列表主要操作controller
 *
 * @author lujia
 * @date 2018/11/07 16:32
 */
@Controller
@RequestMapping("/work/modify")
public class WorkCheckoutController extends OmsBaseController {

    @Autowired
    private ZsNormalFinanceWorkBService zsNormalFinanceWorkBService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    private WorkFlowContextBService workFlowContextBService;
    @Autowired
    private OrderWorkService orderWorkService;


    /**
     * 收单
     *
     * @param operateId 操作类型 14 子公司老总&总部角色操作 8 子公司角色（不含：子公司老总）
     */
    @RequestMapping(value = "/checkout")
    public ModelAndView checkout(Long orderId, Long workId, Integer operateId) {
        ZsCheckOutVO checkOutVO = zsNormalFinanceWorkBService.findCheckoutVo(orderId, workId, operateId);
        ModelAndView mv = new ModelAndView("work/modify/checkout");

        mv.addObject("checkOutVO", checkOutVO);
        return mv;
    }

    /**
     * 保存收单页面
     */
    @RequestMapping(value = "/save/checkout")
    @ResponseBody
    public ResultDTO saveCheckout(@RequestBody ZsCheckOutDTO checkOutDto) throws Exception {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(checkOutDto.getOrderId(), checkOutDto.getWorkId(), OrderConsts.ORDER_OP_TYPE_ADMIN_CHECKOUT);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        // 仓储检查权限
        OrderWork dbOrderWork = orderWorkService.findOrderWorkByKey(checkOutDto.getOrderId(), checkOutDto.getWorkId());
        OperatePermissionVO existsSCMToAuditPartsOPV = orderWorkOperatePermissionBService.existsSCMToAuditParts(checkOutDto.getOrderId(), dbOrderWork.getType());
        if (!existsSCMToAuditPartsOPV.getCanOperate()) {
            return ResultDTO.fail(existsSCMToAuditPartsOPV.getMsg());
        }

        zsNormalFinanceWorkBService.saveFacadeCheckOut(checkOutDto);

        workFlowContextBService.asyncAutoFlow(checkOutDto.getOrderId(), checkOutDto.getWorkId());

        return ResultDTO.success();
    }

}
